<?php

namespace App\Service\playlet;

use App\Model\DataPaymentRefund;
use App\Model\DataSpread;
use App\Service\CommicService;
use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
use Hyperf\DbConnection\Db;

class PaymentRefundService extends CommicService
{
    const cacheDuration = 60 * 15;


    const  returnStatus = [
            '1' => '待处理',
            '2' => '处理中',
            '3' => '退款成功',
            '4' => '退款失败',
        ];

    const refundReason =[
            0 =>'暂无描述',
            1 =>'产品问题，影响使用或效果不佳',
            2 =>'售后问题，无法满足需求',
            3 =>'意愿问题，用户主动退款',
            4 =>'价格问题',
            5 =>'其他原因',
        ];
    const  reqFrom =[
            0 =>'暂无描述',
            1 => '人工客服退款',
            2 => '用户自己发起退款流程',
            3 => '其它'];
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
    }

    const STATUS_ENABLE = 1;


    public function search(array $params)
    {

        $order_id = $params['order_id'] ?? 0;
        $refund_order_id = $params['refund_order_id'] ?? 0;
        $status     = $params['status'] ?? 0;
        $pageSize = $params['pageSize'] ?? 10;
        $page = $params['page'] ?? 1;

        $query = DataPaymentRefund::query()
            ->select(['data_payment_refund.id','data_payment_refund.payment_id','data_payment_refund.handle_user_id',
                'data_payment_refund.user_id', 'data_payment_refund.order_id', 'data_payment_refund.amount', 'data_payment_refund.refund_order_id',
                'data_payment_refund.status', 'data_payment_refund.created_at','data_payment_refund.refund_order_msg',
                'data_payment_refund.updated_at','h_user.real_name as handle_user_real_name','system_user.real_name as user_real_name','data_payment_refund.refund_reason','data_payment_refund.req_from'])
            ->leftJoin('system_user','data_payment_refund.user_id','=','system_user.id')
            ->leftJoin('system_user as h_user','data_payment_refund.handle_user_id','=','h_user.id')
        ;
        if (!empty($order_id)) {
            $query->where(['data_payment_refund.order_id' => $order_id]);
        }
        if (!empty($refund_order_id)) {
            $query->where(['data_payment_refund.refund_order_id' => $refund_order_id]);
        }
        if (!empty($status)) {
            $query->where(['data_payment_refund.status' => $status]);
        }

        $models = $query->orderBy('id', 'desc')->paginate($pageSize, ['*'], 'page', $page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item) {
            $data = $item->toArray();

            $data['status_name'] = self::returnStatus[$item->status];
            $data['refund_reason_name'] = self::refundReason[$item->refund_reason];
            $data['amount'] = $item->amount/100;
            $selectData[] = $data;

        }
        $data = $models->toArray();
        $data['data'] = $selectData;
        return $data;
    }
}